import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/mine/apartment_manage/water_electricity_setting/api.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class ElectricityPriceSetting extends StatefulWidget {
  const ElectricityPriceSetting({super.key});

  @override
  State<ElectricityPriceSetting> createState() => _ElectricityPriceSettingState();
}

class _ElectricityPriceSettingState extends State<ElectricityPriceSetting> {
  final TextEditingController _kwhPriceController = TextEditingController();
  final TextEditingController _kwhManagePriceController = TextEditingController();
  final TextEditingController _kwhServicePriceController = TextEditingController();
  
  Map _priceConfig = {};

  // 获取电费单价配置
  void _getPriceConfig() async {
    Map resData = await waterElectricitySettingApi.getConfigInfo();
    setState(() {
      _priceConfig = resData["data"] ?? {};
      _kwhPriceController.text = _priceConfig["kwhPrice"]?.toString() ?? "";
      _kwhManagePriceController.text = _priceConfig["kwhManagePrice"]?.toString() ?? "";
      _kwhServicePriceController.text = _priceConfig["kwhServicePrice"]?.toString() ?? "";
    });
  }

  // 编辑电费单价
  void _editPrice() async {
    Map data = {
      "waterElectricityConfigId": _priceConfig["waterElectricityConfigId"],
      "kwhPrice": _kwhPriceController.text,
      "kwhManagePrice": _kwhManagePriceController.text,
      "kwhServicePrice": _kwhServicePriceController.text,
    };
    
    await waterElectricitySettingApi.editConfig(data);
    Stacker.pop(true); // 返回上级页面并传递更新标志
  }

  @override
  void initState() {
    _getPriceConfig();
    super.initState();
  }

  @override
  void dispose() {
    _kwhPriceController.dispose();
    _kwhManagePriceController.dispose();
    _kwhServicePriceController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "电费单价设置",
      actions: [
        TextButton(
          onPressed: _editPrice,
          child: const Text("保存", style: TextStyle(color: Colors.white)),
        ),
      ],
      child: Container(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        child: MenuBlock(
          items: [
            MenuBlockItem(
              showArrow: false,
              text: "电费单价",
              suffix: SizedBox(
                width: 120,
                child: TextField(
                  controller: _kwhPriceController,
                  textAlign: TextAlign.right,
                  keyboardType: const TextInputType.numberWithOptions(decimal: true),
                  decoration: const InputDecoration(
                    hintText: "请输入",
                    suffixText: "元/度",
                  ),
                ),
              ),
            ),
            MenuBlockItem(
              showArrow: false,
              text: "管理费",
              suffix: SizedBox(
                width: 120,
                child: TextField(
                  controller: _kwhManagePriceController,
                  textAlign: TextAlign.right,
                  keyboardType: const TextInputType.numberWithOptions(decimal: true),
                  decoration: const InputDecoration(
                    hintText: "请输入",
                    suffixText: "元/度",
                  ),
                ),
              ),
            ),
            MenuBlockItem(
              showArrow: false,
              text: "服务费",
              suffix: SizedBox(
                width: 120,
                child: TextField(
                  controller: _kwhServicePriceController,
                  textAlign: TextAlign.right,
                  keyboardType: const TextInputType.numberWithOptions(decimal: true),
                  decoration: const InputDecoration(
                    hintText: "请输入",
                    suffixText: "元/度",
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
